package com.lancoo.klgcourseware.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.ExpressPracticeModel;
import com.lancoo.klgcourseware.entity.MoreSlideMenuModel;
import com.lancoo.klgcourseware.entity.WordArticulation;
import com.lancoo.klgcourseware.entity.bean.KlgCommonBean;
import com.lancoo.klgcourseware.utils.KlgTimeUtils;
import com.lancoo.klgcourseware.utils.KlgToolUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by pengmi on 2019/8/27 10:21
 * Description:
 */
public class TrainResultActivity extends KlgResulstBaseAcitivy {

    public static final String KEY_CORRECTNUM="correctNum";
    public static final String KEY_TOTALNUM="totalNum";
    private boolean isQuality;
    private List<MoreSlideMenuModel> moreSlideMenuModels;


    @Override
    protected int getContentViewId() {
        return R.layout.klg_activity_train_result;
    }

    @Override
    protected String getTitleName() {

        return klgCommonBean.getKlgName();
    }

    @Override
    protected void setViewAndUpdata() {
        if(mToolbar!=null){
            mToolbar.setBackgroundResource(R.color.transparent);
        }
        initWordMsg();
        moreSlideMenuModels = getIntent().getParcelableArrayListExtra(KEY_TRAINRECORDER);
        isQuality=true;
//        KLog.json(JSON.toJSONString(moreSlideMenuModels));
        for (MoreSlideMenuModel model: moreSlideMenuModels){
            if(model.getIsRecomModule()!=1){
                continue;
            }
            if(model.getIsPass()==0){
                isQuality=false;
                break;
            }
        }
        initDripSound(!isQuality);
        initView();
        initWeepFaceStyle();
    }

    @Override
    public void onClick(View v) {

        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        super.onClick(v);
        int id=v.getId();
       if(id==R.id.tv_train_again){//结束训练
            setResult(isQuality?400:200);
            finish();
        }else if(id==R.id.tv_enter_phonetic_train){//更多学习
           WordArticulation wordArticulation= (WordArticulation) getIntent().getSerializableExtra("articulation");
           List<ExpressPracticeModel> expressPracticeModelList = getIntent().getParcelableArrayListExtra("expressionList");
            KlgMoreActivity.launch(this,0x70,klgCommonBean,wordArticulation,expressPracticeModelList,moreSlideMenuModels);
        }
    }

    public static void launch(Activity activity, KlgCommonBean klgCommonBean, WordArticulation wordArticulation, List<ExpressPracticeModel> expressPracticeModelList, List<MoreSlideMenuModel> trainRecordList, int studyTime){
        Intent mIntent=new Intent(activity, TrainResultActivity.class);
        mIntent.putExtra(KEY_KLGCOMMONBEN,klgCommonBean);
        mIntent.putParcelableArrayListExtra(KEY_TRAINRECORDER,(ArrayList)trainRecordList);
        mIntent.putParcelableArrayListExtra("expressionList",(ArrayList)expressPracticeModelList);
        mIntent.putExtra("articulation",wordArticulation);
        mIntent.putExtra(KEY_TRAINTIME,studyTime);
        activity.startActivityForResult(mIntent,201);
    }

    private void initWeepFaceStyle(){
        ImageView img_weepFace = findViewById(R.id.img_weep_face);
        img_weepFace.setVisibility(View.VISIBLE);
        img_weepFace.setImageResource(isQuality?R.mipmap.klg_icon_pass_on_train:R.mipmap.klg_icon_failure_train);
        findViewById(R.id.ll_score).setVisibility(View.INVISIBLE);
        ImageView img_resultBg = findViewById(R.id.img_result_bg);

        img_resultBg.setImageResource(isQuality?R.mipmap.klg_icon_sprinkle_flowers_no_plage:R.mipmap.klg_icon_drip_rain);
        ImageView img_resultLogo = findViewById(R.id.img_result_logo);
        img_resultLogo.setImageResource(isQuality?R.mipmap.klg_icon_congratulations:R.mipmap.klg_icon_keeptrying);
        img_resultBg.setScaleType(ImageView.ScaleType.MATRIX);
    }

    private void initView(){
        TextView tv_trainTime = findViewById(R.id.tv_train_time);
        int trainTime=getIntent().getIntExtra(KEY_TRAINTIME,0);
        tv_trainTime.setText(getString(R.string.klg_train_time)+KlgTimeUtils.getStudyTime(trainTime));
        TextView tv_closeTrain = findViewById(R.id.tv_train_again);
        tv_closeTrain.setText(isQuality?R.string.klg_close_train:R.string.klg_train_again);
        TextView tv_moreStudy = findViewById(R.id.tv_enter_phonetic_train);
        tv_moreStudy.setText(R.string.klg_more_train_module);
        tv_moreStudy.setVisibility(TextUtils.equals("G",klgCommonBean.getKlgType())?View.GONE:View.VISIBLE);
        findViewById(R.id.tv_enter_phonetic_train_shadow).setVisibility(TextUtils.equals("G",klgCommonBean.getKlgType())?View.GONE:View.VISIBLE);
        findViewById(R.id.center_point).setVisibility(TextUtils.equals("G",klgCommonBean.getKlgType())?View.GONE:View.VISIBLE);
        tv_closeTrain.setOnClickListener(this);
        tv_moreStudy.setOnClickListener(this);
        if(TextUtils.equals("G",klgCommonBean.getKlgType())){
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv_closeTrain.getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.rightMargin=layoutParams.leftMargin;
        }

        boolean isShowModelChange= TextUtils.equals("E",klgCommonBean.getKlgType())||TextUtils.equals("ESP",klgCommonBean.getKlgType());
        if(isShowModelChange){
            tv_moreStudy.setVisibility(View.GONE);
            findViewById(R.id.tv_enter_phonetic_train_shadow).setVisibility(View.GONE);
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv_closeTrain.getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,R.id.rl_spell_result);
            layoutParams.rightMargin= KlgToolUtils.dip2px(this,20);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //退出训练119，102，103
        switch (resultCode){
            case KlgMoreActivity.RESULT_EXIT:
                setResult(400,data);
                break;
            case KlgMoreActivity.RESULT_CANCEL:
                setResult(200,data);
                break;
            case KlgMoreActivity.RESULT_TRAIN:
                setResult(200,data);
                break;
        }
        finish();
    }
}
